package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.Bbs.common.BbsAnalyseDiscuz;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
/**
 * http://www.mop.com/ 猫扑
 * @author allen
 */
public class MopBbsAnalyse implements AnalyseNews{

	BbsAnalyseDiscuz discuzAnalyse = new BbsAnalyseDiscuz();
	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://dzh.mop.com/[0-9]{7,}",
				"http://dzh.mop.com/[0-9]{7,}.html",
				"http://dzh.mop.com/t/.*",
				"http://city.mop.com/[^user]*/.*",
				"http://tt.mop.com/read_[0-9]*_1_0.html",
				"http://.*.mop.com/thread-.*-.*-.*.html",
				"http://.*.mop.com/[0-9]*.html\\?from=[0-9]*",
				"http://.*.mop.com/[a-z]*/[0-9]*.html",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}

		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;

		String updateUrl=null;

		if(url.matches("http://dzh.mop.com/t/.*")){
			String tid=url.substring(url.lastIndexOf("/")+1);
			if(tid.indexOf("?")!=-1){
				tid=tid.substring(0,tid.indexOf("?"));
			}
			updateUrl="http://dzh.mop.com/topic/"+tid+"/readS.do";
			urlMeta=CrawlHTML.responseToURL(updateUrl);
			if(urlMeta.getHtml()==null){
			}else{
				htmltxt = urlMeta.getHtml();
			}

		    Document doc = Jsoup.parse(htmltxt);
			title =doc.select("div.container h1").text();
			if(title==null || title.trim().length()==0){
				title=doc.select("h1").text();
			}
			content = HtmlCleaner.getContentHtml(url, doc.select("div.container div.yharea div.nrarea div.main div#body.tznrP"));
			String tempdate=doc.select("div.container div.yharea div.nrarea div.main div.tzxxP").text();
			date = DateUtils.matchDate(tempdate);
			bbs.setTitle(StringUtils.trimSpace(title));
			bbs.setContent(content);
			bbs.setDate(date);


			String clickComentstr=doc.select("div.container div.infobar div.inner div.pL div.llhfP").text();
			if(clickComentstr!=null && clickComentstr.trim().length()!=0){
				if(clickComentstr.indexOf("浏览量：")!=-1){
					String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("浏览量：")+"浏览量：".length());
					if(clickNumstr.indexOf("　")!=-1){
						clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf("　"));
						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
				if(clickComentstr.indexOf("回复数：")!=-1){
					String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复数：")+"回复数：".length());
					if(commentNumstr.indexOf(" ")!=-1){
						commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf(" "));
						try {
							commentNum=Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
			}
			bbs.setClickNum(clickNum);
			bbs.setCommentNum(commentNum);
			bbs.setUpdateUrl(updateUrl);

			String author=null;
			Elements select = doc.select("div.container div.tzxxP div.inner a");
			if(select!=null && select.size()!=0){
				author=select.get(0).text();
			}
			if(StringUtils.isBlank(author)){
				author = doc.select("div.fl div.c999").text();
			}
			if(StringUtils.isBlank(author)){
				author = doc.select("a.userbyName").text();
			}
			bbs.setAuthor(author);
		}else if(url.matches("http://dzh.mop.com/.*/.*")){
		    Document doc = Jsoup.parse(htmltxt);
			title =doc.select("div.container h1").text();
			content = HtmlCleaner.getContentHtml(url, doc.select("div.container div.yharea div.nrarea div.main div#body.tznrP"));
			if(StringUtils.isBlank(content)){
				content = HtmlCleaner.getContentHtml(url, doc.select("div.article-cont"));
			}
			String tempdate=doc.select("div.container div.yharea div.nrarea div.main div.tzxxP").text();
			date = DateUtils.matchDate(tempdate);
			bbs.setTitle(StringUtils.trimSpace(title));
			bbs.setContent(content);
			bbs.setDate(date);


			String clickComentstr=doc.select("div.container div.infobar div.inner div.pL div.llhfP").text();
			if(clickComentstr!=null && clickComentstr.trim().length()!=0){
				if(clickComentstr.indexOf("浏览量：")!=-1){
					String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("浏览量：")+"浏览量：".length());
					if(clickNumstr.indexOf("　")!=-1){
						clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf("　"));
						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
				if(clickComentstr.indexOf("回复数：")!=-1){
					String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复数：")+"回复数：".length());
					if(commentNumstr.indexOf(" ")!=-1){
						commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf(" "));
						try {
							commentNum=Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
			}
			bbs.setClickNum(clickNum);
			bbs.setCommentNum(commentNum);
			bbs.setUpdateUrl(url);

			String author=null;
			Elements select = doc.select("div.container div.tzxxP div.inner a");
			if(select!=null && select.size()!=0){
				author=select.get(0).text();
			}
			bbs.setAuthor(author);
		}else if(url.matches("http://city.mop.com/.*/.*")){
		    Document doc = Jsoup.parse(htmltxt);
			title =doc.select("div.container h1").text();
			content = HtmlCleaner.getContentHtml(url, doc.select("div.nrP").first());

			String tempdate=doc.select("div.tz_xx div.pL").text();
			date = DateUtils.matchDate(tempdate);
			bbs.setTitle(StringUtils.trimSpace(title));
			bbs.setContent(content);
			bbs.setDate(date);


			String clickComentstr=doc.select("div.titbar li").text();
			if(clickComentstr!=null && clickComentstr.trim().length()!=0){
				if(clickComentstr.indexOf("查看 ")!=-1){
					String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("查看 ")+"查看 ".length());
					if(clickNumstr.indexOf(" ")!=-1){
						clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
				if(clickComentstr.indexOf("回复 ")!=-1){
					String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复 ")+"回复 ".length());
						try {
							commentNum=Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}

				}
			}
			bbs.setClickNum(clickNum);
			bbs.setCommentNum(commentNum);
			bbs.setUpdateUrl(url);
		}else if(url.matches("http://tt.mop.com/read.*_0.html.*")){
		    Document doc = Jsoup.parse(htmltxt);
			title =doc.select("div.container h1").text();
			if(title==null || title.trim().length()==0){
			}
			content = HtmlCleaner.getContentHtml(url, doc.select("div.container div#js-sub-body"));
			String tempdate=doc.select("div.container span.date").text();
			if(tempdate==null || tempdate.trim().length()==0){

			}
			date = DateUtils.matchDate(tempdate);
			bbs.setTitle(StringUtils.trimSpace(title));
			bbs.setContent(content);
			bbs.setDate(date);


			String clickComentstr=doc.select("div.container div.center div.num").text();

			if(clickComentstr!=null && clickComentstr.trim().length()!=0){
				if(clickComentstr.indexOf("点击")!=-1){
					String clickNumstr=clickComentstr.substring(0,clickComentstr.indexOf("点击"));

						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}

				}
				if(clickComentstr.indexOf("|")!=-1){
					String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("|")+1);
					if(commentNumstr.indexOf("回复")!=-1){
						commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf("回复"));
						commentNumstr=commentNumstr.replaceAll(" ", "");
						try {
							commentNum=Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
			}

			bbs.setClickNum(clickNum);
			bbs.setCommentNum(commentNum);
			bbs.setUpdateUrl(url);

			String author=doc.select("div.container div.name").text();
			if(author!=null){
				author=author.replace("楼主：", "").trim();
			}
			bbs.setAuthor(author);



		}else if(url.matches("http://.*.mop.com/thread-.*-.*-.*.html.*")){
		    Document doc = Jsoup.parse(htmltxt);
		    title = JSoupUtils.select(doc,"#thread_subject", "h1");
			content = HtmlCleaner.getContentHtml(url, doc.select("div#postlist div.t_fsz table tbody tr td"));
			String tempdate=doc.select("div#postlist").html();

			date = DateUtils.matchDate(tempdate);
			bbs.setTitle(StringUtils.trimSpace(title));
			bbs.setContent(content);
			bbs.setDate(date);


			String clickComentstr=doc.select("div#postlist table tbody tr td.pls div.hm span").text();
			if(clickComentstr!=null && clickComentstr.trim().length()!=0){
				clickComentstr=clickComentstr.replaceAll(" ", "");
				clickComentstr=clickComentstr.replaceAll("\n", "");
				if(clickComentstr.indexOf("|")!=-1){
					String clickNumstr=clickComentstr.substring(0,clickComentstr.indexOf("|"));
					clickNumstr=clickNumstr.replace("查看:", "");
						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
						String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("|")+1);
						commentNumstr=commentNumstr.replace("回复:", "");
						try {
							commentNum=Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}

				}
			}
			bbs.setClickNum(clickNum);
			bbs.setCommentNum(commentNum);
			bbs.setUpdateUrl(url);



			String author=null;
			Elements select = doc.select("div.authi a.xw1");
			if(select!=null && select.size()!=0){
				author=select.get(0).text();
			}
			bbs.setAuthor(author);
		}else{
		  Document doc = Jsoup.parse(htmltxt);
		  title=doc.select("h1").text();
	
		  content = HtmlCleaner.getContentHtml(url, doc.select(".article"));
		  String author = doc.select(".userbyName").text();
		  if(StringUtils.isBlank(author)){
			  if(doc.select("div.fl a.c999").size()>0){
				  author = doc.select("div.fl a.c999").first().text();
			  }
		  }
		  bbs.setAuthor(author);
		  Elements tmpdate = doc.select(".article-bottom");
		  if(tmpdate.size()>0){
			  date = DateUtils.matchDate(tmpdate.first().text());
		  }
		  if(date==null){
			  tmpdate = doc.select("div.va-m  span.c-blue-gray");
			  if(tmpdate.size()>0){
				  date = DateUtils.matchDate(tmpdate.first().text());
			  }
		  }
		  if(date==null){
			  tmpdate = doc.select("div.fl  span.c-blue-gray");
			  if(tmpdate.size()>0){
				  date = DateUtils.matchDate(tmpdate.first().text());
			  }
		  }
		  if(date==null){
			  tmpdate = doc.select("div.title  p");
			  if(tmpdate.size()>0){
				  date = DateUtils.matchDate(tmpdate.first().text());
			  }
		  }
		  if(date==null){
			  tmpdate = doc.select("div.title  p");
			  if(tmpdate.size()>0){
				  date = DateUtils.matchDate(tmpdate.first().text());
			  }
		  }
		  bbs.setTitle(StringUtils.trimSpace(title));
		  bbs.setContent(content);
		  bbs.setDate(date);
		}

		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return meta;
	}
    
	
}
